﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Xml.Serialization;
using System.IO;

namespace WpfApplication1.Models
{
    public class OrdreCollection
    {
        // Fields
        private ObservableCollection<Ordre> _ordreList = new ObservableCollection<Ordre>();


        // Properties
        public ObservableCollection<Ordre> OrdreList
        {
            get
            {
                if (_ordreList == null)
                {
                    return new ObservableCollection<Ordre>();
                }
                return _ordreList;
            }

            set
            {
                _ordreList = value;
            }
        }
        

        //Methods

        public void fillCollectionFromXml(String filepath)
        {            
            XmlSerializer xs = new XmlSerializer(typeof(ObservableCollection<Ordre>));
            using (StreamReader rd = new StreamReader(filepath))
            {
                OrdreList = xs.Deserialize(rd) as ObservableCollection<Ordre>;
            }
        }

        public void createXmlFromCollection(OrdreCollection oCol, String filepath)
        {
            XmlSerializer xs = new XmlSerializer(typeof(ObservableCollection<Ordre>));
            using (StreamWriter wr = new StreamWriter(filepath))
            {
                xs.Serialize(wr, OrdreList);
            }
        }

        private void LoadOrdreData()
        {
            fillCollection("../../Data/Ordrer.xml");
        }

        private void fillCollection(String filepath)
        {
            XmlSerializer xs = new XmlSerializer(typeof(ObservableCollection<Ordre>));
            using (StreamReader rd = new StreamReader(filepath))
            {
                _ordreList = xs.Deserialize(rd) as ObservableCollection<Ordre>;
            }
        }
    }
}
